package com.example.apple.tianqiyubaoone;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by apple on 17/3/4.
 */

public class Request {
    public void doGet(String ip, String parameter, final Handler handler) {
        final String getUrl = ip + parameter;//拼接地址

        //开启一个线程
        new Thread(new Runnable() {
            public String s;

            @Override
            public void run() {
                try {
                    URL url = new URL(getUrl);
                    //生成HttpURLConnection对象
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setConnectTimeout(3000);
                    httpURLConnection.connect();//连接服务器
                    if (httpURLConnection.getResponseCode()==200){
                        //接受输入流
                        InputStream inputStream = httpURLConnection.getInputStream();
                        //处理接收到的数据,一行一行读取
                        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
                        StringBuffer stringBuffer= new StringBuffer();
                        String readLine = "";
                        while ((readLine = bufferedReader.readLine())!=null){
                            stringBuffer.append(readLine);
                            Log.e("网络返回数据readLine-->",readLine);
                        }
                        inputStream.close();
                        bufferedReader.close();
                        httpURLConnection.disconnect();
                        JSONObject jsonObject = new JSONObject(stringBuffer.toString());
                        Message mag = new Message();
                        mag.obj =jsonObject;
                        Log.e("网络返回数据-->",stringBuffer.toString());
                        handler.sendMessage(mag);
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public  boolean isNetworkAvailable(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (cm == null) {
        } else {
            //如果仅仅是用来判断网络连接
            //则可以使用 cm.getActiveNetworkInfo().isAvailable();
            NetworkInfo[] info = cm.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
}
